package homework;


import java.io.*;
import java.net.Socket;

public class FileClient {
    final static String HOST="localhost";
    final static int PORT=8001;

    private Socket socket;
    private InputStream is;
    private OutputStream os;


    public FileClient(){
        try {
            socket=new Socket(HOST, PORT);
            System.out.println("--文件上传客户端已启动--");
            is=socket.getInputStream();
            os=socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{
        FileClient fileClient=new FileClient();
        File file=new File("ClientText.txt");
        int c=0;
        char ch;

        if (file.exists()){
            //文件输入字符流
            FileInputStream fi=new FileInputStream(file);
            BufferedInputStream bfi=new BufferedInputStream(fi);

            //装饰Socket的输出流
            BufferedOutputStream bos=new BufferedOutputStream(fileClient.os);

            while (c!=-1){
                c=bfi.read();
                ch=(char)c;
                System.out.print(ch);
                bos.write(c);
                bos.flush();
            }
            System.out.println("文件上传成功!");
        }else{
            System.out.println("文件不存在，上传失败!");
        }
    }
}
